<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Student;
use App\Employee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
   
    public function login()
    {
        return view('login');
    }

    public function signin(Request $request)
    {
            $this->validate($request, [
	          'name' => 'required',
	          'password' => 'required',
	          'role' => 'required'
            ]);


    	    $data = $request->only('name','password','role');
    	    $pass=$data['password'];

    	    if($data['role']=='1'){
		    	  $da=Student::where('student_name', $data['name'])
		    	  ->where('password', $pass)
		    	  ->first();
    	    }else{
                  $da=Student::where('employee_name', $data['name'])
		    	  ->where('password', $pass)
		    	  ->first();
    	    }
    	    if($da){
   
	                $request->session()->put('username',$data['name']);
	                $request->session()->put('role',$data['role']);
	                $request->session()->put('useid',$da['student_code']);
		            return response()->json(['success' => true, 'msg' => 'signin success']);
    	    }else{
                   return response()->json(['success' => false, 'msg' => 'account is not exist']);
    	    }
    }

    public function register()
    {
        return view('register');
    }

     public function logout(Request $request)
    {
         $request->session()->flush();
         return redirect("/");
    }

}